// ./swagger/index.js
var path = require("path");
var express = require("express");
var swaggerUi = require("swagger-ui-express");
var swaggerJSDoc = require("swagger-jsdoc");
// 引入放映厅swagger
const projectionHall = require("./projectionHall");
const adminsModel = require("./admins");
const users = require("./users");
// 引入排片swagger
const schedules = require("./schedules");
const cinemas = require("./cinemas");
// 引入电影swagger
const movies =require("./movies");
const options = {
    definition: {
        // 采⽤的 openapi 版本
        openapi: "3.0.0",
        // ⻚⾯基本信息
        info: {
            title: "猫眼后台管理系统", // 设置 swagger 的标题。（项⽬名称）
            version: "1.0.0" //设置版本
        },
        // 配置功能模块
        tags: [
            projectionHall.tags,
            adminsModel.tags,
            users.tags,
            schedules.tags,
            movies.tags,
            cinemas.tags,
        ],
        components: {
            securitySchemes: {
                oauth2: {
                    type: "oauth2",
                    flows: {
                        authorizationCode: {
                            authorizationUrl: "/oauth/dialog",
                            tokenUrl: "/oauth/token"
                        }
                    }
                }
            },
            schemas: {
                // 设置放映厅属性
                ...projectionHall.schemas,
                ...adminsModel.schemas,
                ...users.schemas,
                ...schedules.schemas,
                ...movies.schemas,
                ...cinemas.schemas,
            },
        },
        // 用来描述数据结构
        paths: {
            // 设置放映厅数据结构
            ...projectionHall.paths,
            ...adminsModel.paths,
            ...users.paths,
            ...schedules.schemas,
            ...movies.paths,
            ...cinemas.paths,
        },

    },

    // 去指定项⽬路径下收集 swagger 注释，⽤于⽣成 swagger ⽂档。
    apis: [path.join(__dirname, "../routes/**/*.js")]
};
var swaggerJson = function (req, res) {
    res.setHeader("Content-Type", "application/json");
    res.send(swaggerSpec);
};
const swaggerSpec = swaggerJSDoc(options);
var swaggerInstall = function (app) {
    if (!app) {
        app = express();
    }
    // 根据配置⾃动⽣成 swagger.json ⽂件
    app.get("/swagger.json", swaggerJson);
    // 使⽤ swaggerSpec ⽣成 swagger ⽂档⻚⾯，并开放在指定路由
    app.use("/swagger", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
};
module.exports = swaggerInstall;